package java3_14;

/**
 * @author oldliu
 * @since 1.0
 */
public class JavaClass {
    String name;
    String dirTeacher;
    String manageTeacher;
    String startDate;
    //班级的学生
    String names[] = new String[30];

    //String ids[]=new String[30];
    // String phones[]=new String[30];
    //介绍一下班级情况
    public void selfIntro() {
        System.out.println("这是" + name);
        System.out.println("指导老师" + dirTeacher);
        System.out.println("班主任" + manageTeacher);
        System.out.println("于" + startDate + "开班");
        System.out.println("学生信息如下：");
        for (int i = 0; i < names.length; i++) {
            if (names[0] == null) {
                System.out.println("目前没有学生");
                break;
            }
            if (names[i] == null){
                System.out.println();
                return;
            }
            System.out.print(names[i] + " ");
        }
        System.out.println();
    }

    /**
     * 新生加入班级
     */
    public void add(String newName) {
        //循环，找到数组的空位置
        for (int i = 0; i < names.length; i++) {

            if (names[i] == null) {
                names[i] = newName;
                return;
            }if (names[i]!=null){
                if (names[i].equals(newName)){
                    System.out.println("添加的学生有重复，添加失败!");
                }
            }
        }

        System.out.println("加入失败，没有空位子了");
    }

    //根据名字查找在不在
    public boolean search(String n) {
        for (int i = 0; i < names.length; i++) {
            if (names[i] == null) {
                return false;
            }
            if (names[i].equals(n)) {
                return true;
            }
        }
        return false;
    }

    //按照范围查找学生
    public boolean search2(String n, int start, int end) {
        for (int i = start; i <= end; i++) {
            if (names[i] == null) {
                return false;
            }
            if (names[i].equals(n)) {
                return true;
            }
        }
        return false;
    }
    //查找学生是否存在
    public String find(int index,String name){
        if (names[index].equals(name)){
            return "在";
        }
        return "不在";
    }
    public boolean change(String oldName,String newName){
        if (oldName.equals(newName)){
            return false;
        }
            for (int j = 0; j < names.length; j++) {
                if (names[j] != null) {
                    if (names[j].equals(oldName)) {
                        boolean flag = false;
                        for (int k = 0; k < names.length; k++) {
                            if (newName.equals(names[k])) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){
                            System.out.println("姓名重复，修改失败！");
                            return false;
                        }
                        else {
                            names[j] = newName;
                            return true;
                        }
                    }
                }
            }
        return false;
    }
}

